package com.sj.uabrowser.framework;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.ScaleAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.sj.uabrowser.BuildConfig;
import com.sj.uabrowser.app.Constant;
import com.sj.uabrowser.app.SHASecrete;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.umeng.analytics.MobclickAgent;

import java.util.Date;
import java.util.TreeMap;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseView<P> {

    protected BaseActivity<P> act;
    protected P presenter;
    Unbinder unbinder;
    ProgressDialog progressDialog;
    protected View view;
    protected RxPermissions rxPermissions;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        act = (BaseActivity<P>) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = createPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutID(), container, false);
        unbinder = ButterKnife.bind(this, view);
        progressDialog = new ProgressDialog(act);
        rxPermissions = new RxPermissions(act);
        setView();
        setData();
        return view;
    }



    @Override
    public void showProgressDialog(String text) {
        if (progressDialog != null) {
            progressDialog.setMessage(text);
            progressDialog.show();
        }
    }

    @Override
    public void dismissProgressDialog() {
        if (progressDialog != null)
            progressDialog.dismiss();
    }

    protected void hideKeyBoard(View view) {
        InputMethodManager ims = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
        ims.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
    }

    protected boolean isSlideToBottom(RecyclerView recyclerView) {
        if (recyclerView == null) return false;
        if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange())
            return true;
        return false;
    }


    @Override
    public void onResume() {
        super.onResume();
        if(BuildConfig.UM) {
            MobclickAgent.onPageStart(this.getClass().getName());
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if(BuildConfig.UM) {
            MobclickAgent.onPageEnd(this.getClass().getName());
        }
    }

    @Override
    public void onDestroy() {
        if (presenter != null) {
            presenter.onDestroy();
            presenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public String check(TreeMap<String, Object> params) {
        params.put("appId", Constant.AppId);
        params.put("time", new Date().getTime());
        StringBuilder sign = new StringBuilder();
        for (String s : params.keySet()) {
            sign.append(s + params.get(s));
        }
        sign.append(Constant.AppSecret);
        String sign0 = SHASecrete.getDigestOfString(sign.toString().getBytes()).toUpperCase();
        params.put("sign", sign0);
        return sign0;
    }


    @Override
    public void showToast(String text) {
        Toast.makeText(act, text, Toast.LENGTH_SHORT).show();
    }


    public void startIconAnim(View paramView) {
        ScaleAnimation localScaleAnimation = new ScaleAnimation(0.8F, 1.0F, 0.8F, 1.0F, paramView.getWidth() / 2, paramView.getHeight() / 2);
        localScaleAnimation.setDuration(50L);
        paramView.startAnimation(localScaleAnimation);
//        float f1 = paramView.getWidth() / 2;
//        float f2 = paramView.getHeight() / 2;
//        localScaleAnimation = new ScaleAnimation(1.0F, 0.8F, 1.0F, 0.8F, f1, f2);
//        localScaleAnimation.setInterpolator(new BounceInterpolator());
    }

}
